//
//  MoreInfoViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/31.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "MoreInfoViewController.h"
#import "MemberInfoModel.h"
#import "MemberInfoTypePageModel.h"
@interface MoreInfoViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) ZYKeyboardUtil *keyboardUtil;
@property (nonatomic,strong)MemberInfoTypePageModel * typePageModel;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UILabel * topLabel;
@property (nonatomic, weak)UIView * topInputView;
@property (nonatomic, strong)UILabel * taobaoLabel;
@property (nonatomic, strong)UITextField * taobaoTextField;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UILabel * JDLabel;
@property (nonatomic, strong)UITextField * JDTextField;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UILabel * QQLabel;
@property (nonatomic, strong)UITextField * QQTextField;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UILabel * weixinLabel;
@property (nonatomic, strong)UITextField * weixinTextField;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UILabel * emailLabel;
@property (nonatomic, strong)UITextField * emailTextField;
@property (nonatomic, weak)UIButton * sureButton;

@property (nonatomic,strong)MemberInfoModel * pageModel;
@property (nonatomic,strong)ParaModel * para;
@end

@implementation MoreInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACK_COLOR;
    [self getPageModel];
    [self setupNavBar];
    [self setupUI];
}


-(void)dealloc
{
    self.JDTextField.delegate = nil;
    self.QQTextField.delegate = nil;
    self.emailTextField.delegate = nil;
    self.taobaoTextField.delegate = nil;

    self.weixinTextField.delegate = nil;

}

- (void)configKeyBoardRespond {
    self.JDTextField.delegate = self;
    self.QQTextField.delegate = self;
    self.emailTextField.delegate = self;
    self.taobaoTextField.delegate = self;
    
    self.weixinTextField.delegate = self;
    self.keyboardUtil = [[ZYKeyboardUtil alloc] initWithKeyboardTopMargin:MARGIN_KEYBOARD];
    __weak MoreInfoViewController *weakSelf = self;
#pragma explain - 全自动键盘弹出/收起处理 (需调用keyboardUtil 的 adaptiveViewHandleWithController:adaptiveView:)
#pragma explain - use animateWhenKeyboardAppearBlock, animateWhenKeyboardAppearAutomaticAnimBlock will be invalid.
    [_keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
        [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.JDTextField, weakSelf.QQTextField, weakSelf.emailTextField, weakSelf.taobaoTextField, weakSelf.weixinTextField, nil];
    }];
    
#pragma explain - 获取键盘信息
    [_keyboardUtil setPrintKeyboardInfoBlock:^(ZYKeyboardUtil *keyboardUtil, KeyboardInfo *keyboardInfo) {
        NSLog(@"\n\n拿到键盘信息 和 ZYKeyboardUtil对象");
    }];
}


- (void)setupNavBar{
    
    self.title = @"更多信息";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = TOTAL_COLOR;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    UILabel * topLabel = [UILabel new];
    
    topLabel.textColor = ColorWithRGB(0x7f7f7f);
    topLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    topLabel.text = @"为保证借款申请顺利通过，请务必填写真实信息！";
    
    self.topLabel = topLabel;
    
    [self.view addSubview:self.topLabel];
    
    
    UIView * topInputView = [UIView new];
    
    topInputView.backgroundColor = [UIColor whiteColor];
    topInputView.layer.borderColor = [ColorWithRGB(0xe6e6e6) CGColor];
    topInputView.layer.borderWidth = 0.5 * SCREENH6;
    
    self.topInputView = topInputView;
    
    [self.view addSubview:self.topInputView];
    
    self.taobaoLabel = [self setupLabelWith:@"淘宝账号"];
    
    [self.topInputView addSubview:self.taobaoLabel];
    
    self.taobaoTextField = [self setupTextField:@"请输入淘宝账号"];
    
    [self.topInputView addSubview:self.taobaoTextField];
    
    self.oneLineView = [self setupLineView];
    
    [self.topInputView addSubview:self.oneLineView];

    self.JDLabel = [self setupLabelWith:@"京东账号"];
    
    [self.topInputView addSubview:self.JDLabel];
    
    self.JDTextField = [self setupTextField:@"请输入京东账号"];
    
    [self.topInputView addSubview:self.JDTextField];
    
    self.twoLineView = [self setupLineView];
    
    [self.topInputView addSubview:self.twoLineView];
    
    self.QQLabel = [self setupLabelWith:@"QQ账号"];
    
    [self.topInputView addSubview:self.QQLabel];
    
    self.QQTextField = [self setupTextField:@"请输入QQ账号"];
    
    [self.topInputView addSubview:self.QQTextField];
    
    self.threeLineView = [self setupLineView];
    
    [self.topInputView addSubview:self.threeLineView];

    self.weixinLabel = [self setupLabelWith:@"微信账号"];
    
    [self.topInputView addSubview:self.weixinLabel];
    
    self.weixinTextField = [self setupTextField:@"请输入微信账号"];
    
    [self.topInputView addSubview:self.weixinTextField];
    
    self.fourLineView = [self setupLineView];
    
    [self.topInputView addSubview:self.fourLineView];
    
    self.emailLabel = [self setupLabelWith:@"常用邮箱"];
    
    [self.topInputView addSubview:self.emailLabel];
    
    self.emailTextField =[self setupTextField:@"请输入常用邮箱"];
    
    [self.topInputView addSubview:self.emailTextField];
    
    
    UIButton * sureButton = [UIButton new];
    //300 * 42
    [sureButton setTitle:@"保存" forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didSaveButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(13 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.topInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.height.mas_equalTo(257 * SCREENH6);
        make.top.mas_equalTo(self.topLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.taobaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topInputView).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.topInputView).offset(8 * SCREENH6);
    }];
    
    [self.taobaoTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.taobaoLabel);
        make.trailing.mas_equalTo(self.topInputView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topInputView).offset(2 * SCREENH6);
        make.trailing.mas_equalTo(self.topInputView).offset(-2 * SCREENH6);
        make.height.mas_equalTo(0.5 * SCREENH6);
        make.top.mas_equalTo(self.topInputView).offset(51 * SCREENH6);
    }];
    
    [self.JDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.taobaoLabel);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.JDTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.width.height.mas_equalTo(self.taobaoTextField);
        make.centerY.mas_equalTo(self.JDLabel);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.leading.trailing.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(51 * SCREENH6);
    }];
    
    [self.QQLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.JDLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.QQTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.leading.trailing.mas_equalTo(self.JDTextField);
        make.centerY.mas_equalTo(self.QQLabel);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(51 * SCREENH6);
    }];
    
    [self.weixinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.QQLabel);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.weixinTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.height.leading.mas_equalTo(self.QQTextField);
        make.centerY.mas_equalTo(self.weixinLabel);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(51 * SCREENH6);
    }];
    
    [self.emailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.weixinLabel);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.weixinTextField);
        make.centerY.mas_equalTo(self.emailLabel);
    }];
    
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topInputView.mas_bottom).offset(60 * SCREENH6);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
    }];
}



- (void)didSaveButton{
    if(self.emailTextField.text.length == 0)
    {
        [self showHint:@"请输入常用邮箱"];
        return;
    }
    self.para.taobaoAccount = self.taobaoTextField.text;
    self.para.jdAccount = self.JDTextField.text;
    self.para.qqAccount = self.QQTextField.text;
    self.para.wechatAccount = self.weixinTextField.text;
    self.para.mailAddress = self.emailTextField.text;
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile updateExtraInfo] Param:[self.para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            [self showHint:@"保存成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}



- (UILabel *)setupLabelWith:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ColorWithRGB(0x303c41);
    label.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
    label.text = title;
    
    return label;
}


- (UITextField *)setupTextField:(NSString *)title{
    
    UITextField * textField = [[UITextField alloc] init];
    
    textField.borderStyle = UITextBorderStyleNone;
    textField.backgroundColor = [UIColor clearColor];
    textField.layer.borderColor = [[UIColor clearColor] CGColor];
    textField.textAlignment = NSTextAlignmentRight;
    //设置光标颜色
    textField.tintColor = ColorWithRGB(0xcecece);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xcecece);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    //修改占位符文字颜色
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    textField.textColor = ColorWithRGB(0x303c41);
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    
    return textField;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xdbdbdb);
    
    return lineView;
}



- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}








-(void)getPageModel
{
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile loadCurrentPersonalInfo] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.pageModel = [[MemberInfoModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
            self.para.taobaoAccount = self.pageModel.extraJsonInfo.taobaoAccount;
            self.para.jdAccount = self.pageModel.extraJsonInfo.jdAccount;
            self.para.qqAccount = self.pageModel.extraJsonInfo.qqAccount;
            self.para.wechatAccount = self.pageModel.extraJsonInfo.wechatAccount;
            self.para.mailAddress = self.pageModel.extraJsonInfo.mailAddress;
            
            [self updateView];
            
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}


-(void)updateView
{

    self.taobaoTextField.text = self.pageModel.extraJsonInfo.taobaoAccount;
    self.JDTextField.text = self.pageModel.extraJsonInfo.jdAccount;
    self.QQTextField.text = self.pageModel.extraJsonInfo.qqAccount;
    self.weixinTextField.text = self.pageModel.extraJsonInfo.wechatAccount;
    self.emailTextField.text = self.pageModel.extraJsonInfo.mailAddress;

    
  
}

#pragma mark delegate
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.JDTextField resignFirstResponder];
    [self.QQTextField resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self.taobaoTextField resignFirstResponder];
    [self.weixinTextField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self.JDTextField resignFirstResponder];
    [self.QQTextField resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self.taobaoTextField resignFirstResponder];
    [self.weixinTextField resignFirstResponder];
    return YES;
}
-(ParaModel*)para
{
    if(!_para)
    {
        _para = [ParaModel new];
    }
    return _para;
}

@end
